package models

import "gorm.io/gorm"

// 用户表
type User struct {
	gorm.Model
	Mobile          string `gorm:"type:varchar(11);comment:'手机号'"`
	Password        string `gorm:"type:varchar(32);comment:'密码'"`
	Nickname        string `gorm:"type:varchar(20);comment:'昵称'"`
	MemberStatus    int    `gorm:"type:int(1);default:1;comment:'会员状态 1:非会员 2:普通 3:中级 4:高级'"`
	MemberExpiredAt string `gorm:"varchar(30);comment:'会员有效期直至'"`
}

func (u *User) GetUserById(db *gorm.DB, id int64) error {
	return db.Where("id = ?", id).Limit(1).Find(&u).Error
}

type MemberTypes struct {
	gorm.Model
	Title         string  //标题 普通会员、中级会员、高级会员
	Price         float64 //价格
	Duration      int64   //会员时长/天
	PrerogativeId int64   //特权类型ID
	Status        int64   `gorm:"type:int(1);default:1;comment:'会员状态 1:有效 2:失效'"`
}

func (mt *MemberTypes) GetTypeInfoById(db *gorm.DB,
	id int64) (typesList []MemberTypes, err error) {

	query := db.Debug()
	if id != 0 {
		query = query.Where("id=?", id).Limit(1)
	}

	err = query.Find(&typesList).Error

	return typesList, err
}

func (mt *MemberTypes) GetTypeById(db *gorm.DB, id int64) error {
	return db.Where("id=?", id).Limit(1).Find(&mt).Error
}

// 充值订单记录表
type Orders struct {
	gorm.Model
	UserId       int64   //用户ID
	OrderSn      string  //订单编号
	MemberTypeId int64   //会员套餐
	Num          int     //购买数量
	Total        float64 //支付总金额
	PayType      int64   //支付方式 1-支付宝 2-银行卡
	Status       int64   `gorm:"type:int(1);default:0;comment:'充值状态 0:待支付 1:已支付 2-已取消'"`
}

func (o *Orders) CrateOrder(db *gorm.DB) error {
	return db.Create(&o).Error
}

func (o *Orders) ListOrders(db *gorm.DB, userId int64) (listOrders []Orders, err error) {

	err = db.Where("user_id = ?", userId).
		Order("created_at desc").
		Find(&listOrders).Error

	return listOrders, err
}

func (o *Orders) UpdateStatus(db *gorm.DB, orderSn string, status int64) error {
	return db.Model(&o).Where("order_sn=?", orderSn).Update("status", status).Error
}

// 视频剧集播放记录
type PlayRecord struct {
	gorm.Model
	UserId    int64  `gorm:"type:int;not null;comment:用户ID"`
	EpisodeId int64  `gorm:"type:int;not null;comment:电视剧ID"`
	VideoId   int64  `gorm:"type:int;not null;comment:视频ID"`
	Seconds   string `gorm:"type:varchar(20);comment:观看时长"`
}

// 查看剧集播放,是某人剧集的播放记录
func (pr *PlayRecord) GetEpisodePlayRecord(db *gorm.DB, userId int64) error {
	return db.Where("user_id = ?", userId).Limit(1).Find(&pr).Error
}
